﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KamLib
{

    /// <summary>
    /// Уникальный идентификатор поля таблицы
    /// </summary>
    public class DbFieldID
    {
        /// <summary>
        /// Уникальный идентификатор поля в запросе (алиас)
        /// </summary>
        public string Name;
        /// <summary>
        /// Уникальный идентификатор таблицы (уникальный алиас)
        /// </summary>
        public string TableAlias;
        /// <summary>
        /// Имя поля в таблице
        /// </summary>
        public string DbName;

        public bool IsKey;
        public bool IsGroup;
        public QueryArgs.SortDirection SortDir;

        /// <summary>
        /// </summary>
        /// <param name="names">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
        public DbFieldID(string names)
        {
            if (names.isnull()) return;

            var ss = names.Split(':');
            if (ss.Length > 1)
            {
                names = ss[0];
                foreach (var type in ss[1].Split(',').Select(a => a.TrimLower()))
                {
                    switch (type)
                    {
                        case "key": IsKey = true; break;
                        case "group": IsGroup = true; break;
                        case "asc": SortDir = QueryArgs.SortDirection.ASC; break;
                        case "desc": SortDir = QueryArgs.SortDirection.DESC; break;
                    }
                }
            }
            else
                names = names.Trim();

            var dbnames = names;

            ss = names.Split('=');
            if (ss.Length > 1)
            {
                Name = ss[0].Trim();
                dbnames = ss[1].Trim();
            }

            DbName = dbnames;

            ss = dbnames.Split('.');
            if (ss.Length > 1)
            {
                TableAlias = ss[0].Trim();
                DbName = ss[1].Trim();
            }

            Name = Name ?? DbName;
        }

        public DbFieldID(string name, string tid, string dbname)
        {
            Name = name;
            TableAlias = tid;
            DbName = dbname;
        }

        public string SqlName()
        {
            return DbName.isnull() ? Name : TableAlias.isfull() ? TableAlias + "." + DbName : DbName;
        }


        public class List : List<DbFieldID>
        {
            public List(IEnumerable<DbFieldID> items) : base(items) { }

            /// <summary>
            /// </summary>
            /// <param name="names0">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
            public List(string names0)
            {
                Add(new DbFieldID(names0));
            }

            /// <summary>
            /// </summary>
            /// <param name="names0">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
            public List(string names0, string names1)
            {
                Add(new DbFieldID(names0));
                Add(new DbFieldID(names1));
            }

            /// <summary>
            /// </summary>
            /// <param name="names0">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
            public List(string names0, string names1, string names2)
            {
                Add(new DbFieldID(names0));
                Add(new DbFieldID(names1));
                Add(new DbFieldID(names2));
            }

            /// <summary>
            /// </summary>
            /// <param name="names">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
            public List(params string[] names)
            {
                AddRange(names.Select(a => new DbFieldID(a)));
            }


            public DbFieldID this[string name]
            {
                get { return Find(a => String.Equals(a.Name, name, StringComparison.CurrentCultureIgnoreCase)); }
            }


            public string FirstName()
            {
                return Count > 0 ? this[0].Name : null;
            }

            public string LastName()
            {
                return Count > 0 ? this[Count - 1].Name : null;
            }

        }
    }

}
